package com.hongyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.SysLoginInfo;
import com.hongyi.system.domain.dto.SysLogininforDto;
import com.hongyi.system.domain.vo.SysLoginInfoVo;
import com.hongyi.system.service.ISysLoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录日志控制器
 *
 * @author DingHao
 */
@ApiSort(value = 7)
@Api(tags = "登录日志接口")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLoginInfoController extends BaseController {

    @Autowired
    private ISysLoginInfoService sysLoginInfoService;


    /**
     * 登录日志分页数据
     *
     * @param sysLogininforDto 日志对象
     * @param pageQuery 分页数据
     * @return 日志分页数据
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "登录日志列表")
    @ApiImplicitParam(name = "info", value = "日志对象", paramType = "query", dataTypeClass = SysLoginInfo.class)
    @SaCheckPermission("monitor:logininfor:list")
    @GetMapping(value = "/list")
    public PageData<SysLoginInfoVo> list(SysLogininforDto sysLogininforDto, PageQuery pageQuery) {
        return sysLoginInfoService.queryPageLogininforList(sysLogininforDto, pageQuery);
    }

    /**
     * 导出登录日志(流方式)
     *
     * @param sysLogininforDto
     * @param response
     */
    @ApiOperation(value = "导出登录日志")
    @Log(title = "导出登录日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:logininfor:export")
    @PostMapping(value = "/exportByStream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportByStream(SysLogininforDto sysLogininforDto, HttpServletResponse response) {
        List<SysLoginInfoVo> sysLoginInfoVos = sysLoginInfoService.queryLogininforList(sysLogininforDto);
        ExcelUtil.exportExcel(sysLoginInfoVos, "登录日志", SysLoginInfoVo.class, response);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "最后登录时间")
    @GetMapping(value = "/lastLogin")
    public JsonResult<String> lastLogin() {
        return JsonResult.ok("查询成功",sysLoginInfoService.selectLastLoginByUserName());
    }

    /**
     * 登录日志删除
     *
     * @param ids 日志ID数组
     * @return 结果
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "登录日志删除")
    @ApiImplicitParam(name = "ids", value = "日志ID数组", paramType = "path", allowMultiple = true, required = true)
    @Log(title = "登录日志删除", businessType = BusinessType.DELETE)
    @SaCheckPermission("monitor:logininfor:remove")
    @DeleteMapping(value = "/{ids}")
    public JsonResult<Void> remove(@PathVariable Long[] ids) {
        return toResult(sysLoginInfoService.deleteLogininforByIds(ids));
    }

    /**
     * 登录日志清空
     *
     * @return 结果
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "登录日志清空")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @SaCheckPermission("monitor:logininfor:remove")
    @DeleteMapping(value = "/clean")
    public JsonResult<Void> clean() {
        sysLoginInfoService.cleanLogininfor();
        return JsonResult.ok();
    }

}
